spring配置mybatis的sqlsessionfactory
<!--读入配置文件 --> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <value>classpath*:jdbc.properties</value> </property> </bean> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="${jdbc.driverClassName}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean> <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <!-- 文件映射器,指定类文件 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"/> <!-- 自动扫描mapping.xml文件 --> <property name="mapperLocations" value="classpath:com/chinasoft/ssm/mapper/*.xml"></property> </bean>
<!--下面两种方法,一种是自动是扫描对应的service类,一种是通过注入sqlsessionFactory的方式来获取一个SqlsessionTemplate--> <!-- DAO接口所在包名,Spring会自动查找其下的类 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--set注入--> <property name="basePackage" value="com.chinasoft.ssm.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean>
<!-- 配置SqlSessionTemplate -->
<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<!-- 通过构造函数注入 -->
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>